//
//  MPHomePageViewController.m
//  SearchPeople
//
//  Created by zk on 16/5/6.
//
//

#import "MPHomePageViewController.h"
#import "MPHomePageTableViewCell.h"
#import "MPClueViewController.h"
#import "MPFavoritiesTableViewController.h"
#import "MPDocumentHttpClient.h"
#import <SVProgressHUD.h>
#import "MPSubmitTableViewController.h"
#import "MPSettingsManager.h"

@interface MPHomePageViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *Datasource;
@property (nonatomic, getter=isLogin) BOOL login;
@end

@implementation MPHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Tableview];
    UIBarButtonItem *creat = [[UIBarButtonItem alloc] initWithTitle:@"创建" style:UIBarButtonItemStylePlain target:self action:@selector(create)];
    self.navigationItem.rightBarButtonItem = creat;
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self dataSource];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private

- (void)dataSource{
    self.login = [[MPSettingsManager getInstance].userDefault objectForKey:@"user"] ? YES : NO;
    if (self.login) {
        [[MPDocumentHttpClient shareInstance] GetDocumentWithType:1 WithSuccessBlock:^(MPRequest *request, MPResponse *response) {
            NSDictionary * dic = response.result;
            self.Datasource = [dic[@"data"] mutableCopy];
            [[MPDocumentHttpClient shareInstance] GetDocumentWithType:0 WithSuccessBlock:^(MPRequest *request, MPResponse *response) {
                NSDictionary * dic = response.result;
                NSArray *array = dic[@"data"];
                for (NSDictionary *dictionary in array) {
                    [self.Datasource addObject:dictionary];
                }
                [self.tableview reloadData];
            } WithFailedBlock:^(MPRequest *request, MPResponse *response) {
                [SVProgressHUD showErrorWithStatus:REQUEST_FAILE];
            }];
        } WithFailedBlock:^(MPRequest *request, MPResponse *response) {
            [SVProgressHUD showErrorWithStatus:REQUEST_FAILE];
        }];
        
        
    }
}

- (void)Tableview{
    self.automaticallyAdjustsScrollViewInsets = NO;//cell距离顶端没有距离
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.tableFooterView = [UIView new];
}

#pragma mark - UITableView delegate & datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MPHomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MPHomePageTableViewCell"];
    [cell ConfigWithDic:self.Datasource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"HomePage" bundle:nil];
    MPClueViewController *targerVC = [story instantiateViewControllerWithIdentifier:@"MPClueViewController"];
    targerVC.dic = self.Datasource[indexPath.row];
    [self.navigationController pushViewController:targerVC animated:YES];
}
- (void)create{
    if (self.login) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Favorites" bundle:nil];
        MPSubmitTableViewController *targeVC = [story instantiateViewControllerWithIdentifier:@"MPSubmitTableViewController"];
        targeVC.title = @"创建档案";
        [self.navigationController pushViewController:targeVC animated:YES];
    }else{
        //        [SVProgressHUD showErrorWithStatus:@"请登录"];
        [SVProgressHUD showInfoWithStatus:@"请登录"];
    }
    
}

#pragma mark - Setter & Getter

- (NSMutableArray *)Datasource
{
    if (!_Datasource) {
        _Datasource = [[NSMutableArray alloc] init];
    }
    return _Datasource;
}
@end
